package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.config.type.DecrConfigTypeSortBo;
import com.zhuss.bean.vo.core.config.type.DetailConfigTypeVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ConfigTypeDao;
import com.zhuss.core.db.entity.ConfigType;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 下移配置类型 processor
 * @Author: zhushanshuo
 * @Date: 2020/03/20 21:55
 */
@Component("configTypeDecrSort")
public class ConfigTypeDecrSort extends AbstractProcessor<Object, DecrConfigTypeSortBo> implements Processor<Object, DecrConfigTypeSortBo> {

    @Autowired
    private ConfigTypeDao configTypeDao;
    
    @Autowired
	@Qualifier("configTypeRead")
	private ConfigTypeRead configTypeRead;
    
    /**
     * 校验
     */
	@Override
    public void check(DecrConfigTypeSortBo bo) throws OceanException {
    	Long configTypeId = bo.getConfigTypeId();
    	Integer incr = bo.getIncr();
    	DetailConfigTypeVo entity = (DetailConfigTypeVo) configTypeRead.detail(configTypeId);
    	Integer sort = entity.getSort();
    	sort = incr + sort;
    	if (sort < 1) {
    		throw new OceanException(ExceptionCode.请预设顺序值);
    	}
    	bo.setSort(sort);
    	
    	ConfigType configType = configTypeRead.getBySort(sort);
    	if (configType != null) {
    		bo.setOther(configType.getConfigTypeId());
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DecrConfigTypeSortBo param) throws OceanException {
    	Long configTypeId = param.getConfigTypeId();
    	Long other = param.getOther();
    	String value = ((DetailConfigTypeVo) configTypeRead.detail(configTypeId)).getValue();
    	
    	ConfigType record = new ConfigType();
    	record.setConfigTypeId(configTypeId);
    	record.setSort(param.getSort());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	configTypeDao.updateByPrimaryKeySelective(record);
    	
    	if (other != null) {
    		record = new ConfigType();
    		record.setConfigTypeId(other);
    		record.setSort(param.getSort() - param.getIncr());
    		
    		record.setModifierId(param.getConcurrentUserId());
    		record.setModifier(param.getConcurrentUserName());
    		record.setModifyTime(new Date());
    		configTypeDao.updateByPrimaryKeySelective(record);
    	}
    	param.setName(value);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DecrConfigTypeSortBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.配置类型顺序移动.key());
    	record.setTargetId(param.getConfigTypeId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
